package com.hainatong.shiphelper.module.warehouse.fragment;

import android.os.Bundle;
import android.view.View;
import cn.bertsir.zbar.Qr.ScanResult;
import cn.bertsir.zbar.QrManager;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.hainatong.shiphelper.*;
import com.hainatong.shiphelper.databinding.FragmentRukuAddListBinding;
import com.hainatong.shiphelper.mainhome.ComFragmentActivity;
import com.hainatong.shiphelper.mainhome.fragment.CommonResultFragment;
import com.hainatong.shiphelper.module.warehouse.adapter.ItemRukuAdapter;
import com.hainatong.shiphelper.module.warehouse.entity.ImpaShelfCodeBean;
import com.hainatong.shiphelper.module.warehouse.entity.RukuGoodsInfo;
import com.hainatong.shiphelper.module.warehouse.viewmodel.RuKuLogViewModel;
import com.hainatong.shiphelper.utils.DialogUitl;
import com.hainatong.shiphelper.utils.GsonUtil;
import com.hainatong.shiphelper.utils.RecyclerViewHelper;
import com.hainatong.mvvmbase.rxbus.RxBusBaseMessage;
import com.jeremyliao.liveeventbus.LiveEventBus;

public class RukuAddFragment extends MvvmLazyFragment<FragmentRukuAddListBinding, RuKuLogViewModel>
{
    private static String RUKU_ADD_TAG = "RukuAddFragment";

    public static String RUKU_REFRESH = "RukuRefresh";

    public static int RUKU_ADD_CODE = 0x51;

    // 货架 id
    private String shelfUuid;

    private ItemRukuAdapter rukuAdapter;

    private RukuGoodsInfo delRukuGoods;

    //private List<RukuGoodsInfo> rukuGoodsList;

    public static RukuAddFragment newInstance(String scanCode)
    {
        Bundle bundle = new Bundle();
        bundle.putString( RUKU_ADD_TAG , scanCode );
        RukuAddFragment addFragment = new RukuAddFragment();
        addFragment.setArguments( bundle );
        return addFragment;
    }

    @Override
    public int getLayoutId()
    {
        return R.layout.fragment_ruku_add_list;
    }

    @Override
    public int getBindingVariable()
    {
        return 0;
    }

    @Override
    protected void initParameters()
    {
        Bundle args = getArguments();
        if(  args != null )
        {
            shelfUuid = args.getString(RUKU_ADD_TAG , "");
        }
    }

    @Override
    protected void onRetryBtnClick()
    {
    }

    @Override
    protected void initView(View view)
    {
        rukuAdapter = new ItemRukuAdapter();
        //rukuGoodsList = new ArrayList<RukuGoodsInfo>();
        RecyclerViewHelper.addLinerLayoutMgrNoDecoration(viewDataBinding.recyclerview);
        viewDataBinding.recyclerview.setAdapter( rukuAdapter );


        LiveEventBus.get( RUKU_REFRESH, RxBusBaseMessage.class)
                     .observeSticky(this, rxMsg -> {

                        if( rxMsg.getCode() == RUKU_ADD_CODE )
                        {
                            RukuGoodsInfo rukuGoods = (RukuGoodsInfo) rxMsg.getObject();

                            //每次 添加完 就刷新 一边
                            rukuAdapter.addData(rukuGoods);
                            rukuAdapter.notifyDataSetChanged();
                            LogUtils.e( "rukuGoodsList size =" + rukuAdapter.getData().size() );
                            setTotalCount( rukuAdapter.getData().size() );
                        }

                     } );


        rukuAdapter.addChildClickViewIds(R.id.btn_delete);
        rukuAdapter.setOnItemChildClickListener((adapter, view1, position) -> {

            delRukuGoods = rukuAdapter.getItem( position );

            if( view1.getId() == R.id.btn_delete )
            {
                DialogUitl.showSimpleDialog(getActivity(), "确定要删除吗？", (dialog, content) -> {
                    //从列表中删除 即 走 出库接口  删掉 入库的 商品
                    deleteRukuGoods(delRukuGoods);
                });
            }
        });
        //先扫描 物资  添加 商品
        viewDataBinding.fabAdd.setOnClickListener( v -> {
            //先扫描 物资  在跳转 到 添加 商品
            startScanQrcode();
        } );
        //点击 完成
        viewDataBinding.tvOk.setOnClickListener( v -> {

            //rukuGoodsList.clear();
            rukuAdapter.getData().clear();
            rukuAdapter.notifyDataSetChanged();

            //切换完成 页面
            RxBusBaseMessage rxMsg = new RxBusBaseMessage(CommonResultFragment.RUKU_SUCCESS_RESULT , "RukuSuccess");
            /*
            LiveEventBus.get(Constants.VIEW_TYPE_COMMON_RESULT_TAG , RxBusBaseMessage.class)
                        .post( rxMsg );
            */
            // 显示 完成 页面
            ((ComFragmentActivity)getActivity()).showCommonResult(rxMsg);
        } );


    }

    //出库  移除列表
    private void deleteRukuGoods(RukuGoodsInfo delGoodsInfo)
    {
        viewModel.deleteRukuLogByUuid( delGoodsInfo.getUuid() );
    }

    @Override
    protected void lazyLoad()
    {
        //rukuAdapter.setList( rukuGoodsList);
        //setTotalCount( rukuGoodsList.size() );
        // 移除 即 出库 结果
        viewModel.getDelRukuResult().observe(this,result -> {
            if( result.isSuccess() )
            {
                //移除 .getData().
                rukuAdapter.remove(delRukuGoods);
                //rukuAdapter.remove();
                rukuAdapter.notifyDataSetChanged();
                setTotalCount( rukuAdapter.getData().size() );
            }
            else
            {
                ToastUtils.showShort( result.getMsg() );
            }
        });

    }

    private void setTotalCount(int size)
    {
        String totalCount = String.format( getString( R.string.has_ruku_count ), String.valueOf( size ) );
        viewDataBinding.tvSelectTotal.setText( totalCount );
    }

    public void startScanQrcode()
    {
        QrManager.getInstance().init( App.getScanQrConfigByType(2) ).startScan(getActivity(), new QrManager.OnScanResultCallback() {
            @Override
            public void onScanSuccess(ScanResult result)
            {
                LogUtils.e(TAG, "onScanSuccess: " + result.getContent());
                if( result.getType() == ScanResult.CODE_QR)
                {
                    //ToastUtils.showShort( result.getContent() );

                    //拿到物资 code   跳转 入库 详情
                    ImpaShelfCodeBean codeBean = new ImpaShelfCodeBean( shelfUuid ,result.getContent() );
                    codeBean.setFromRuKuAdd( true );
                    //跳转 入库 添加商品
                    ComFragmentActivity.startViewByParam( getActivity(), Constants.VIEW_TYPE_RUKU_TASK, GsonUtil.obj2json(codeBean) );
                }
                else
                {
                    ToastUtils.showShort( result.getContent() );
                }
            }
        });
    }

}
